#include "udp_server.hpp"
#include <memory>
#include <bits/stdc++.h>
#include <cstdio>
#include <ctype.h>

using namespace std;
using namespace ns_server;

void Usage(char* proc)
{
    cout << "Usage:\n\t" << proc << " port\n" << endl;
}

//dome1
string transactionString(string message)
{
    //将数据中的小写字母转为大写字母
    string mes = message;
    transform(mes.begin(), mes.end(), mes.begin(), ::toupper);
    return mes;
}

//dome2
static bool isPass(const std::string &command)
{   
    bool pass = true;
    auto pos = command.find("rm");
    if(pos != std::string::npos) pass=false;
    pos = command.find("mv");
    if(pos != std::string::npos) pass=false;
    pos = command.find("while");
    if(pos != std::string::npos) pass=false;
    pos = command.find("kill");
    if(pos != std::string::npos) pass=false;
    return pass;
}

string excuteCommand(string command)//command是一个命令
{
    //1.安全检查
    if(!isPass(command)) return "You are bad man!";

    //2.调用popen函数对command进行命令解析，并读取结果放入到返回值中
    FILE* fp = popen(command.c_str(), "r");
    if(fp == nullptr)
    {
        return "None";
    }
    //获取结果
    string result;
    char buffer[2048];
    while(fgets(buffer, sizeof(buffer), fp) != nullptr)
    {
        result += buffer;
    }
    pclose(fp);

    return result;
}

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }
    //unique_ptr<UdpServer> usvr(new UdpServer(atoi(argv[1])));
    //unique_ptr<UdpServer> usvr(new UdpServer(transactionString, atoi(argv[1])));
    //unique_ptr<UdpServer> usvr(new UdpServer(excuteCommand, atoi(argv[1])));
    unique_ptr<UdpServer> usvr(new UdpServer(atoi(argv[1])));//dome3--群聊


    //usvr->InitServer();//服务器初始化
    usvr->Start();

    return 0;
}